package com.gdcp.controller.backend;

import com.gdcp.beans.*;
import com.gdcp.common.ResponseModel;
import com.gdcp.config.RequestHolder;
import com.gdcp.mapper.ProductMapper;
import com.gdcp.service.OrderItemService;
import com.gdcp.service.OrderService;
import com.gdcp.service.ProductService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Controller
@RequestMapping("/render/orderItemManager")
public class OrderItemController_backend {

    @Autowired
    ResponseModel responseModel;
    @Autowired
    OrderItemService orderItemService;
    @Autowired
    OrderService orderService;
    @Autowired
    CommonCode commonCode;
    @Autowired
    ProductService productService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> list(int pageSize, int pageNumber, String sortName, String sortOrder){
        Map<String,Object> model=commonCode.getList(pageSize, pageNumber, sortName, sortOrder);
        User user= (User) RequestHolder.getSession("user");

        for (Role role: user.getRoles()){
            if (role.getName().equals("ROLE_ADMIN")){
                return orderItemService.findAll(model);
            }
        }

        Integer shop_id=user.getShop_id();
        model.put("shop_id",shop_id);
        return orderItemService.findPageBreakByCondition(model, shop_id);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> remove(Integer[] ids){
        if (ids==null){
            return responseModel.setModel(false,"请至少选择一条记录");
        }

        return orderItemService.removeByPrimaryKey(ids);
    }

    @RolesAllowed({"ROLE_ADMIN"})
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(OrderItem orderItem){
        int orderItemCount=orderItemService.insert(orderItem);
        if (orderItemCount<=0){
            return responseModel.setModel(true,"添加失败，注意订单号(order_no)是否存在或者该用户(user_id)是否存在");
        }
        return responseModel.setModel(true,"添加成功");
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(OrderItem orderItem){
        try{
            //System.out.println(orderItem);
            Integer status=orderItem.getShop_status();
            User user= (User) RequestHolder.getSession("user");
            if (status==50||status==10||status==60){
                for (Role role: user.getRoles()){
                    if (!role.getName().equals("ROLE_ADMIN")){
                        return responseModel.setModel(false,"你的权限不够！");
                    }
                }
            }

            if (status==40){
                orderItem.setUser_status(40);
                /*减少商品库存*/
                //System.out.println(orderItem.getProduct_id());
                OrderItem orderItem1=orderItemService.getInformation(orderItem.getId());
                Product product1=productService.getInformation(orderItem1.getProduct_id());
                Product product=new Product();
                product.setId(product1.getId());
                //System.out.println(product1.getStock()+","+orderItem1.getQuantity());
                product.setStock(product1.getStock()-orderItem1.getQuantity());
                productService.updateInformationByOrderId(product);
            }else if (status==0){
                orderItem.setUser_status(60);
            }
            int orderItem_count=orderItemService.updateInformation(orderItem);
            /*Order order=new Order();
            if (status==0) {
                order.setStatus(0);
            }

            int order_count=orderService.updateInformation(order);&&order_count>0*/

            if (orderItem_count<=0){
                return responseModel.setModel(false,"修改失败");
            }
            
            return responseModel.setModel(true,"修改成功");
        }catch (Exception e){
            //System.out.println(e.toString());
            return responseModel.setModel(false,e.toString());
        }
    }

    @RequestMapping(value = "/get/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> get(@PathVariable Integer id){
        OrderItem orderItem=orderItemService.getInformation(id);
        //System.out.println(orderItem);
        if (orderItem==null){
            return responseModel.setModel(false,"获取失败");
        }
        return responseModel.setModel(true,orderItem);
    }

    @RequestMapping(value = "/search",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> search(String searchText, String sortName, String sortOrder){
        String str = "["+searchText+"]";
        JSONArray json= JSONArray.fromObject(str);
        Map<String, Object> param=new HashMap<>();
        param.put("sortName", sortName);
        param.put("sortOrder", sortOrder);

        for(int i=0;i<json.size();i++){
            JSONObject jsonOne = json.getJSONObject(i);
            param.put("user_id", jsonOne.getString("user_id"));
            param.put("order_no", jsonOne.getString("order_no"));
            param.put("product_name", jsonOne.getString("product_name"));
        }

        User user= (User) RequestHolder.getSession("user");
        for (Role role: user.getRoles()){
            if (role.getName().equals("ROLE_ADMIN")){
                return orderItemService.searchInformation(param);
            }
        }

        param.put("shop_id",user.getShop_id());
        if (user.getShop_id()==0){
            return responseModel.setModel(false,"信息错误");
        }

        return orderItemService.searchInformationByShopId(param,user.getShop_id());
    }
}